package com.kanshu.ksgb.fastread.doudou.module.book.presenter;

import com.kanshu.ksgb.fastread.doudou.app.Xutils;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.RetrofitHelper;
import com.kanshu.ksgb.fastread.doudou.common.net.rx.BaseObserver;
import com.kanshu.ksgb.fastread.doudou.common.util.NetUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.module.book.bean.ChapterBean;
import com.kanshu.ksgb.fastread.doudou.module.book.retrofit.BookService;
import com.kanshu.ksgb.fastread.doudou.module.book.retrofit.requestparams.BookPageParams;
import com.kanshu.ksgb.fastread.doudou.module.book.retrofit.requestparams.ChapterRequestParams;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wcy on 2017\7\4 0004.
 */

public class BookModel {
    List<Disposable> mDisposables = new ArrayList<>();
    BookService mBookService;
    public BookModel() {
        RetrofitHelper helper = RetrofitHelper.getInstance();
        mBookService = helper.createService(BookService.class);
    }

    public void getChapterList(BookPageParams params, final INetCommCallback<BaseResult<List<ChapterBean>>> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            callback.onError(Constants.ErrCode.NetErr, "no net");
            return;
        }
        mBookService.getChapterList(params, "1")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io()).subscribe(new BaseObserver<List<ChapterBean>>() {
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mDisposable.isDisposed()) {
                    return;
                }
                mDisposables.add(mDisposable);
                callback.onError(code, errDesc);
            }

            @Override
            public void onResponse(BaseResult<List<ChapterBean>> info, List<ChapterBean> userBean, Disposable disposable) {
                if (mDisposable.isDisposed()) {
                    return;
                }
                mDisposables.add(disposable);
                if (BaseResult.isNotNull(info)) {
                    callback.onResponse(info);
                }
            }
        });
    }
    private HashSet<String> mHashSet = new HashSet<>();
    public void getChapterContent(final ChapterRequestParams params,final INetCommCallback<BaseResult<ChapterBean>> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            callback.onError(Constants.ErrCode.NetErr, "no net");
            return;
        }
        if (mHashSet.contains(params.content_id)) {
            return;
        }
        mHashSet.add(params.content_id);
        mBookService.getChapterContent(params)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io()).subscribe(new BaseObserver<ChapterBean>() {
            @Override
            public void onNext(BaseResult<ChapterBean> tBaseResult) {
                if (tBaseResult.result != null && tBaseResult.result.status != null) {
                    onResponse(tBaseResult, tBaseResult.result.data, mDisposable);
                } else {
                    onError(Constants.ErrCode.NoData, "no data");
                }
                mHashSet.remove(params.content_id);
            }

            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mDisposable.isDisposed()) {
                    return;
                }
                mDisposables.add(mDisposable);
                callback.onError(code, errDesc);
                mHashSet.remove(params.content_id);
            }

            @Override
            public void onResponse(BaseResult<ChapterBean> info, ChapterBean userBean, Disposable disposable) {
                if (mDisposable.isDisposed()) {
                    return;
                }
                mDisposables.add(disposable);
                if (BaseResult.isNotNull(info)) {
                    callback.onResponse(info);
                }
            }
        });
    }



    public void cancel() {
        Utils.dispose(mDisposables);
        mHashSet.clear();
    }


}
